
function loadingImage(src, callback) {
    var img = new Image();
    img.src = src;
    if (img.complete) {
        callback.call(img, true);
    } else {
        img.onload = function () {
            callback.call(img, true);
        };
        img.onerror = function () {
            callback.call({}, false);
        };
    }
};



function resizeImg(img, iwidth, iheight) {
    var image = img;
    var boxWH = {};
    if (image.width > 0 && image.height > 0) {
        boxWH.width = image.width;
        boxWH.height = image.height;
        if (boxWH.width > iwidth) {
            boxWH.height = (boxWH.height * iwidth) / boxWH.width;
            boxWH.width = iwidth;

        }
        if (boxWH.height > iheight) {
            boxWH.width = (boxWH.width * iheight) / boxWH.height;
            ;
            boxWH.height = iheight;
        }
    }
    return boxWH;
};



/*给要预览的图片或者元素上加 class="ksd-imgbox"和data-src='图片地址即可'*/
function mkImageShow() {
    var ow = window.innerWidth;
    var oh = window.innerHeight;
    $(window).resize(function () {
        var $imgbox = $(".ksd-imgcontainer");
        var imgsrc = $imgbox.find(".ksd-imgcnt").find("img").attr("src");
        if (!imgsrc) return;
        var xbit = this.innerWidth / ow;
        var ybit = this.innerHeight / oh;
        var width = $imgbox.data("width") * 1;
        var height = $imgbox.data("height") * 1;
        var wb = width * xbit;
        var yb = height * ybit;

        loadingImage(imgsrc, function (ok) {
            if (ok) {
                var imgjson = resizeImg(this, wb, yb);
                var cwidth = imgjson.width;
                var cheight = imgjson.height;
                $imgbox.find(".ksd-imgcnt").stop(true, true).animate({
                    width: cwidth,
                    height: cheight,
                    marginLeft: "-" + (cwidth / 2) + "px",
                    marginTop: "-" + (cheight / 2) + "px"
                });
                $imgbox.find(".ksd-imgcnt").find("img").attr("width", cwidth).attr("height", cheight);
            } else {
                alert("远程服务商禁止下载图片。无法提供预览");
                $(".ksd-imgoverlay,.ksd-imgcontainer").remove();
            }
        });
    });

    $("#markdownbox").off("click").on("click", "img", function (e) {
        var imgsrc = $(this).attr("src");
        var flag = $(this).data("flag")
        if(flag==1)return;
        if (!imgsrc) imgsrc = $(this).data("src").replace(",", "");
        $(".ksd-imgoverlay,.ksd-imgcontainer").remove();
        loadingImage(imgsrc, function (ok) {
            if (ok) {
                var bw = 1440;
                var bh = 768;
                if (bw >= ow) bw = ow;
                if (bh >= oh) bh = oh - 100;
                var imgjson = resizeImg(this, bw, bh);
                var width = imgjson.width;
                var height = imgjson.height;
                var html = "<div class='ksd-imgcontainer' data-width='" + width + "' data-height='" + height + "'>" +
                    "  		<div class='ksd-imgcnt'  style='width:" + width + "px;height:" + height + "px;margin-left:-" + (width / 2) + "px;margin-top:-" + (height / 2) + "px;'>" +
                    "  			<img class='animated bounceIn' src='" + imgsrc + "' onerror='imgErrorTip(this)' width='" + width + "' height='" + height + "'>" +
                    "  		</div><a href='javascript:void(0);' class='ksd-imgclose'><i class='iconfont iconremove1'></i></a>" +
                    "  	</div>";
                $("body").append(html).append("<div class='ksd-imgoverlay'></div>");
                $(".ksd-imgcontainer").off("click").on("click", function () {
                    $(this).next().remove();
                    $(this).remove();
                });
                clearInterval(this.ctttimer);
            } else {
                alert("服务商禁止下载图片或者加载失败，无法提供预览");
                $(".ksd-imgoverlay,.ksd-imgcontainer").remove();
                clearInterval(this.ctttimer);
            }
        });
        e.stopPropagation();
    });


};
